<?php

/**
 * 数据操作
 * @author Cuil <lntschampion@163.com>
 * @date 2016-2-14
 */
class MY_Model extends CI_Model {

    public $db;
    public $type;
    public $table;
    protected $slave;
    protected $master;

    function __construct() {
        parent::__construct();
        $this->conn();
    }

    //初始化
    function conn($params = array()) {
        $type = 'default';
        //$type =(isset($params['dbname']) && $params['dbname'] )? $params['dbname'] : 'default' ;
        $this->db = $this->slave = $this->master = $this->load->database($type, true);
    }

    /**
     * ----------------------------------------------------
     * 获取数据列表 
     * ----------------------------------------------------
     * @param $field(string)(array) 查询字段
     * @param $where (array) 查询数组
     * @param $limit(int) 查询条数
     * @param $offset(int) 开始位置
     * @param $order(string) 排序
     * @auther Cuil <lntschampion@163.com>
     * @version 2015-02-14
     */
    public function getList($field = '*', $where = array(), $limit = 1, $offset = Null, $order = '', $group = '') {
        $this->slave->from($this->table);
        $this->slave->select($field);
        $this->like($where);
        $this->_in($where);
        $this->_or($where);
        $this->slave->where($where);
        if (intval($limit) > 0) {
            $this->slave->limit($limit, $offset);
        }

        if (!empty($order)) {
            $this->slave->order_by($order);
        }
        if (!empty($group)) {
            $this->slave->group_by($group);
        }
        $rec = $this->slave->get();
        //echo $this->slave->last_query();
        return $rec->result_array();
    }

    /**
     * ----------------------------------------------------------------
     * 多表关联查多条数据
     * ----------------------------------------------------------------
     * @param $tables (array) array('goods' => 'goods' ,'库名'	=> '别名');
     * @param $join (array) array('goods.goods_id = detail.goods_id');
     * @param $field (array) 查询字符串 goods.id
     * @param $where (array) 查询条件  array('goods.goods_id' => 1)
     * @param $limit (int) 查询条数
     * @param $offset (int) 偏移量
     * @param $order (string)排序
     * @return (array) 二维数组
     * @author		Cuil <lntschampion@163.com>
     * @date		2016-02-14
     */
    public function getJoinList($tables, $join, $field = "*", $where, $limit = 1, $offset = 0, $order = NULL, $group = NULL) {
        $this->slave->select($field);
        $i = 0;
        foreach ($tables as $k => $v) {
            if ($i == 0) {
                $this->slave->from($k . " As " . $v);
            } else {
                $this->slave->join($k . " As " . $v, $join[$i - 1], 'left');
            }
            $i++;
        }
        $this->like($where);
        $this->_in($where);
        $this->_or($where);
        $this->slave->where($where);
        $this->slave->limit($limit, $offset);
        if (isset($order)) {
            $this->slave->order_by($order);
        }
        if (!empty($group)) {
            $this->slave->group_by($group);
        }
        $query = $this->slave->get();
        //echo $this->slave->last_query();
        return $query->result_array();
    }

    /**
     *
     * 模糊搜索	
     * @param $where (array) 模糊搜索条件  array('or_where' => array('username' => $data['key']),);
     * @modify		
     * @author		Cuil <lntschampion@163.com>
     * @date		2016-02-14
     */
    public function like(&$where) {
        if (isset($where['search']) && !empty($where['search']) && is_array($where['search'])) {
            foreach ($where['search'] as $k => $v) {
                foreach ($v as $key => $val) {
                    $k = trim($k);
                    $this->slave->$k($key, $val);
                }
            }
            unset($where['search']);
        }
    }

    /**
     * IN搜索
     * @param $where (array) 模糊搜索条件  $where['in'] = array('userid' => $ids);
     * @author		Cuil <lntschampion@163.com>
     * @date		2016-02-14
     */
    public function _in(&$where) {
        if (isset($where['in']) && !empty($where['in']) && is_array($where['in'])) {
            foreach ($where['in'] as $k => $v) {
                $this->slave->where_in($k, $v);
            }
            unset($where['in']);
        }
    }

    /**
     * OR搜索
     * @param $where (array) 模糊搜索条件 $where['or'] = array('userid' => '1', 'username' => 'c');
     * @author		Cuil <lntschampion@163.com>
     * @date		2016-02-14
     */
    public function _or(&$where) {
        if (isset($where['or']) && !empty($where['or']) && is_array($where['or'])) {
            foreach ($where['or'] as $k => $v) {
                if (is_array($v)) {
                    foreach ($v as $ork => $orv) {
                        $this->slave->or_where($k, $orv);
                    }
                } else {
                    $this->slave->or_where($k, $v);
                }
            }
            unset($where['or']);
        }
    }

    /**
     * --------------------------------------------------
     * 获取一条数据
     * ---------------------------------------------------
     * @param $field(string)(array) 查询字段
     * @param $where (array) 查询数组
     * @param $order(string) 排序描述			
     * @author		Cuil <lntschampion@163.com>
     * @date		2016-02-14
     */
    public function getRow($field = '*', $where = array(), $order = '', $distinct = false) {
        $this->slave->from($this->table);
        $this->slave->select($field);
        if ($distinct) {
            $this->slave->distinct();
        }

        $this->like($where);
        $this->_in($where);
        $this->_or($where);
        $this->slave->where($where);
        $this->slave->limit(1);
        if (!empty($order)) {
            $this->slave->order_by($order);
        }
        $rec = $this->slave->get();
        //echo $this->slave->last_query();
        return $rec->row_array();
    }

    /**
     * --------------------------------------------------
     * 获取一条数据
     * ---------------------------------------------------
     * @param $field(string)(array) 查询字段
     * @param $where (array) 查询数组
     * @param $order(string) 排序描述
     * @author		Cuil <lntschampion@163.com>
     * @date		2016-02-14
     */
    public function getOne($field = '*', $where = array(), $order = '', $distinct = false) {
        $result = $this->getRow($field, $where, $order, $distinct);

        if (!empty($result)) {
            foreach ($result as $k => $v) {
                return $v;
            }
        }

        return '';
    }

    /**
     * --------------------------------------------------
     * 获取一条数据
     * ---------------------------------------------------
     * @param $field(string)(array) 查询字段
     * @param $where (array) 查询数组
     * @param $order(string) 排序描述
     * @author		Cuil <lntschampion@163.com>
     * @date		2016-02-14
     */
    public function getJoinOne($tables, $join, $field = "*", $where, $order = NULL) {
        $result = $this->getJoinRow($tables, $join, $field, $where, $order);

        foreach ($result as $k => $v) {
            return $v;
        }
        return '';
    }

    /**
     * ----------------------------------------------------------------
     * 多表关联查询一条数据
     * ---------------------------------------------------------------
     * @param $tables (array) array('goods' => 'goods' ,'库名'	=> '别名');
     * @param $join (array) array('goods.goods_id = detail.goods_id');
     * @param $field (array) 查询字符串 goods.id
     * @param $where (array) 查询条件  array('goods.goods_id' => 1)
     * @param $order (string)排序
     * @return (array) 一维数组
     * @author		Cuil <lntschampion@163.com>
     * @date		2016-02-14
     */
    public function getJoinRow($tables, $join, $field = "*", $where, $order = NULL) {
        $this->slave->select($field);
        $i = 0;
        foreach ($tables as $k => $v) {
            if ($i == 0) {
                $this->slave->from($k . " As " . $v);
            } else {
                $this->slave->join($k . " As " . $v, $join[$i - 1], 'left');
            }
            $i++;
        }
        $this->like($where);
        $this->_in($where);
        $this->_or($where);
        $this->slave->where($where);
        $this->slave->limit(1);
        if (isset($order)) {
            $this->slave->order_by($order);
        }
        $query = $this->slave->get();
        //echo $this->slave->last_query();
        return $query->row_array();
    }

    /**
     * 修改某一字段值-递增递减 
     * @author		Cuil <lntschampion@163.com>
     * @date		2016-02-14
     */
    public function setFile($data, $where) {
        $this->master->where($where);
        foreach ($data as $k => $v) {
            $this->master->set($k, $v, FALSE);
        }
        return $this->master->update($this->table);
    }

    /**
     *
     * 插入数据
     * @param $data (array) 添加的数据
     * @param $new_id (Bool) 是否返回ID
     * @return (bool)(int) 返回 Bool 或 插入 自增ID
     * @author		Cuil <lntschampion@163.com>
     * @date		2016-02-14
     */
    public function insertData($data, $new_id = FALSE) {
        $rec = $this->master->insert($this->table, $data);
        //echo $this->master->last_query();exit;
        if ($rec && $new_id) {
            return $this->master->insert_id();
        }
        return $rec;
    }

    /*     * *
     * @param $data
     * @return mixed
     * @author Cuil <lntschampion@163.com>
     * @date 2016-02-14
     */

    public function insert_batch($data) {
        $result = $this->db->insert_batch($this->table, $data);
// 		echo $this->db->last_query();
        return $result;
    }

    /**
     *
     * 插入数据
     * @param $data (array) 添加的数据
     * @param $new_id (Bool) 是否返回ID
     * @return (bool)(int) 返回 Bool 或 插入 自增ID
     * @author Cuil <lntschampion@163.com>
     * @date  2016-02-14
     */
    public function insertDatatotable($data, $new_id = FALSE, $table = '') {
        $table = $table ? $table : $this->table;
        $rec = $this->master->insert($table, $data);
        //echo $this->master->last_query();exit;
        if ($rec && $new_id) {
            return $this->master->insert_id();
        }
        return $rec;
    }

    function saveData($data) {
        if (empty($data['id'])) {
            $id = $this->insertData($data, true);
        } else {
            $where = array(
                'id' => $data['id'],
            );
            $id = $data['id'];
            unset($data['id']);
            $this->updateData($data, $where);
        }
        return $id;
    }

    /**
     * ----------------------------------------------------------
     * 编辑数据库条目
     * ---------------------------------------------------------
     * @param $where (array) 条件
     * @param $data (array) 更新数据			
     * @return (bool)
     * @author		Cuil <lntschampion@163.com>
     * @date		2016-02-14
     */
    public function updateData($data, $where) {
        return $this->master->update($this->table, $data, $where);
    }

    /**
     * ---------------------------------------------
     * 物理删除数据			
     * -------------------------------------------
     * @param $where 删除where条件
     * @return bool
     * @author		Cuil <lntschampion@163.com>
     * @date		2016-02-14
     */
    public function delData($where) {
        $r = $this->master->delete($this->table, $where);
        //echo $this->master->last_query();
        return $r;
    }

    public function last_query($table = 'slave') {
        $table = ( $table == 'slave') ? 'slave' : 'master';
        return $this->$table->last_query();
    }

    public function __destruct() {
        if (DBLOG) {
            $query = $this->get_queries();
            foreach ($query as $k => $sql) {
                $sql = str_replace(array("\r", "\n"), ' ', $sql);
                Logs::db($sql);
            }
        }
    }

    /**
     * 得到所有查询语句
     * @param book $dump 是否在页面直接打印
     * @return array $queries
     */
    public function get_queries($dump = false) {
        $queries = array();
        if (!empty($this->slave->queries)) {
            $queries = $this->slave->queries;
            if ($dump) {
                Dump($this->slave->queries);
            }
        }
        return $queries;
    }

    public function getPageList($field, $where, $limit, $offset, $order, $group) {

        $data = array('total_page' => "0", 'list' => array());

        $count = $this->get_one('count(*)', $where);
        if ($count == 0) {
            return $data;
        }
        $data['total_page'] = ceil($count / $limit);

        $result = $this->getList($field, $where, $limit, $offset, $order, $group);

        if (!empty($result)) {
            $data['list'] = $result;
        }

        return $data;
    }

    //记录积分奖励
    public function saveIntegral($uid, $type) {

        //查询奖励类型
        $where['type'] = $type;
        $this->slave->from('bya_user_integral_type');
        $this->slave->select('*');
        $this->slave->where($where);
        $rec = $this->slave->get();
        $type = $rec->result_array();

        if (!empty($type)) {

            //查询是否第一次登录
            if ($type[0]['type'] == 'login') {

                $where['type'] = $type[0]['type'];
                $where['uid'] = $uid;
                $where["FROM_UNIXTIME(sent_time,'%Y%m%d')"] = date('Ymd', time());

                $this->slave->from('bya_system_message');
                $this->slave->select('*');
                $this->slave->where($where);
                $rec = $this->slave->get();
                $message = $rec->result_array();

                //如果第一次登录继续执行
                if (!empty($message)) {
                    return false;
                }
            }

            //查询用户积分
            $this->slave->from('user');
            $this->slave->select('*');
            $this->slave->where(array('id' => $uid));
            $rec = $this->slave->get();
            $user = $rec->result_array();


            //保存积分记录
            $data['uid'] = $uid;
            $data['integral'] = $type[0]['integral'];
            $data['addtime'] = time();

            $data['before'] = $user[0]['integral'];
            $data['after'] = $user[0]['integral'] + $type[0]['integral'];

            $data['type_id'] = $type[0]['id'];
            $data['type_name'] = $type[0]['name'];

            //保存积分记录
            $this->master->insert('bya_user_integral', $data);

            //更新用户积分值
            $list['integral'] = $data['after'];
            $this->master->update('user', $list, array('id' => $uid));

            //发送系统通知
            $message = '恭喜您通过 ' . $type[0]['name'] . ' 获得 ' . $type[0]['integral'] . ' 积分！';
            $this->saveMessage($uid, '', $message, $type[0]['type']);
        }
    }

    //记录系统消息
    public function saveMessage($uid, $title, $message, $type = '') {

        if ($title == '') {
            $title = '倍优通知消息';
        }

        $data['uid'] = $uid;
        $data['title'] = $title;
        $data['message'] = $message;
        $data['sent_time'] = time();
        $data['type'] = $type;

        $this->master->insert('bya_system_message', $data);
    }

    //执行sql语句
    function exec($sql) {
        $result = $this->db->query($sql);
        return $result;
    }

    //查询list data
    function querylist($sql) {

        $result = array();
        $query = $this->db->query($sql);
        if ($query) {
            foreach ($query->result_array() as $row) {
                $result[] = $row;
            }
        }
        return $result;
    }

}
